<?php
$q=$_GET["q"];
$hint="";
if(strlen($q)>0)
{
include_once ("Login/databaseconnection.php");
require_once ("Login/config.php");
$dbname = $db;
	$arratab="";
	$delarray="";
	$sqlque ="SHOW TABLES FROM $dbname";
	$resulttable=mysql_query($sqlque);
while($rowtable=mysql_fetch_row($resulttable))
{
	$tna=substr($rowtable[0],0,3);
	$sta="del";
	if(strcmp($tna,$sta)==0)
	{
			if(empty($arratab))
		{
			$arratab=$rowtable[0];
		}
		else
		{
			$arratab=$arratab.",".$rowtable[0];
		}
	}
}
$dta= str_replace(","," ",$arratab);
$delTables=explode(" ",$dta);
for($t=0;$t<count($delTables);$t++)
{
	$cherows=mysql_query("SELECT * FROM $delTables[$t]");
	if(mysql_num_rows($cherows)>0)
	{
		if(empty($delarray))
		{
			$delarray=$delTables[$t];
		}
		else
		{
		$delarray=$delarray.",".$delTables[$t];
		}
	}
}
$delarrayl=str_replace(","," ",$delarray);
$avaiTables=explode(" ",$delarrayl);
$sql=mysql_query("SELECT * FROM  $q");
for($t=0;$t<count($avaiTables);$t++)
{
if($avaiTables[$t]==$q)
{
$text=ucwords(substr(str_replace("_"," ",$avaiTables[$t]),7));
echo '<font color="red">'.$text."</font>|";
}
else
{
$text=ucwords(substr(str_replace("_"," ",$avaiTables[$t]),7));
echo '<font color="#00000">'.$text."</font>|";
}
}
if(mysql_num_rows($sql)!=0)
{
$selectedtab=$avaiTables[0];

$hint=$hint.'<div style =" width:134%; overflow-x:scroll;">';
$hint=$hint.'<table width="134%" border="1" class="tablefontv">';
$hint=$hint.'<tr  bgcolor="#6AB5FF">';
$hint=$hint.'<td width="20">Drop</td>';
$hint=$hint.'<td width="20">Re put</td>';
$que="SHOW COLUMNS FROM $q";
$resultcol=mysql_query($que);
$totcol=mysql_num_rows($resultcol);
$colnamearray="";
if($q=="deleted_lab")
	{
	for($cv=0;$cv<10;$cv++)
	{
	if($rowcolu=mysql_fetch_row($resultcol))
{
if(empty($arratab))
{
$colnamearray=$rowcolu[0];
}
else
{
$colnamearray=$colnamearray.",".$rowcolu[0];
}
$cname=str_replace("_"," ",$rowcolu[0]);
$hint=$hint.'<td>'.$cname.'</td>';
}
	}
	}
	else if($q=="deleted_teacher")
	{
	for($cv=0;$cv<21;$cv++)
	{
	if($rowcolu=mysql_fetch_row($resultcol))
{
if(empty($arratab))
{
$colnamearray=$rowcolu[0];
}
else
{
$colnamearray=$colnamearray.",".$rowcolu[0];
}
$cname=str_replace("_"," ",$rowcolu[0]);
$hint=$hint.'<td>'.$cname.'</td>';
}
	}
	}
	else
	{
for($i=0;$i<$totcol;$i++)
{
if($rowcolu=mysql_fetch_row($resultcol))
{
if(empty($arratab))
{
$colnamearray=$rowcolu[0];
}
else
{
$colnamearray=$colnamearray.",".$rowcolu[0];
}
$cname=str_replace("_"," ",$rowcolu[0]);
$hint=$hint.'<td>'.$cname.'</td>';
}
}
}
$hint=$hint.'</tr>';
for($i=0;$i<count($avaiTables);$i++)
{
if($avaiTables[$i]==$q)
{
$next=$avaiTables[$i+1];
$back=$avaiTables[$i-1];
}
}
$hint=$hint."<input type='hidden' name='delTable' value='$next'/>";
$hint=$hint."<input type='hidden' name='backTable' value='$back'/>";
while($row=mysql_fetch_array($sql))
{
$hint=$hint.'<tr bgcolor="#999999">';
if($q=="deleted_school")
{
$d=$row['Deleted_date'];
$c=$row['Census_Number'];

	$hint=$hint.'<td width="29"><a href="index.php?action=deletes&Census_Number='.$c.'&Deleted_Date='.$d.'">';
	$hint=$hint.'<img src="images/b_deltbl.png" width="25"/></a></td>';
    $hint=$hint.'<td width="29"><a href="index.php?action=adds&Census_Number='.$c.'&Deleted_Date='.$d.'">';
	$hint=$hint.'<img src="images/b_insrow.png" width="25"/></a></td>';
}
if($q=="deleted_teacher")
{
$d=$row['Deleted_date'];
$c=$row['T_NIC_Number'];
	$hint=$hint.'<td width="29"><a href="index.php?action=deletet&T_NIC_Number='.$c.'&Deleted_Date='.$d.'">';
	$hint=$hint.'<img src="images/b_deltbl.png" width="25"/></a></td>';
    $hint=$hint.'<td width="29"><a href="index.php?action=addt&T_NIC_Number='.$c.'&Deleted_Date='.$d.'">';
	$hint=$hint.'<img src="images/b_insrow.png" width="25"/></a></td>';
}
if($q=="deleted_training_details")
{
$s=$row['Teacher_NIC'];
$d=$row['Deleted_date'];
$c=$row['Training_Id'];
	$hint=$hint.'<td width="29"><a href="index.php?action=deletetd&Training_Id='.$c.'&Teacher_NIC='.$s.'&Deleted_Date='.$d.'">';
	$hint=$hint.'<img src="images/b_deltbl.png" width="25"/></a></td>';
    $hint=$hint.'<td width="29"><a href="index.php?action=addtd&Training_Id='.$c.'&School_Id='.$s.'&Deleted_Date='.$d.'">';
	$hint=$hint.'<img src="images/b_insrow.png" width="25"/></a></td>';
}
if($q=="deleted_competition")
{
$s=$row['School_Id'];
$d=$row['Date'];
$c=$row['Competition_Id'];
	$hint=$hint.'<td width="29"><a href="index.php?action=delete&Competition_Id='.$c.'&School_Id='.$s.'&Date='.$d.'">';
	$hint=$hint.'<img src="images/b_deltbl.png" width="25"/></a></td>';
    $hint=$hint.'<td width="29"><a href="index.php?action=add&Competition_Id='.$c.'&School_Id='.$s.'&Date='.$d.'">';
	$hint=$hint.'<img src="images/b_insrow.png" width="25"/></a></td>';
}
if($q=="deleted_exam")
{
$s=$row['School_Id'];
$d=$row['Year'];
$c=$row['Exam_Name'];
	$hint=$hint.'<td width="20"><a href="index.php?action=deletee&Exam_Name='.$c.'&School_Id='.$s.'&Year='.$d.'"><img src="images/b_deltbl.png" width="25"/></a></td>';
     $hint=$hint.'<td width="20"><a href="index.php?action=adde&Exam_Name='.$c.'&School_Id='.$s.'&Year='.$d.'"><img src="images/b_insrow.png" width="25"/></a></td>';
}
if($q=="deleted_programmes")
{
$s=$row['School_Id'];
$p=$row['Programme_Id'];
$d=$row['Year'];
$g=$row['Grade'];
	$hint=$hint.'<td width="20"><a href="index.php?action=deletep&Grade='.$g.'&School_Id='.$s.'&Programme_Id='.$p.'&Year='.$d.'"><img src="images/b_deltbl.png" width="25"/></a></td>';
    $hint=$hint.'<td width="20"><a href="index.php?action=addp&Grade='.$g.'&School_Id='.$s.'&Programme_Id='.$p.'&Year='.$d.'"><img src="images/b_insrow.png" width="25"/></a></td>';
}
if($q=="deleted_resource_details")
{
$s=$row['Resource_Id'];
$d=$row['Deleted_date'];
$c=$row['Serial_Number'];
		$hint=$hint.'<td width="20"><a href="index.php?action=deleter&Serial_Number='.$c.'&Resource_Id='.$s.'&Deleted_Date='.$d.'"><img src="images/b_deltbl.png" width="25"/></a></td>';
    $hint=$hint.'<td width="20"><a href="index.php?action=addr&Serial_Number='.$c.'&Resource_Id='.$s.'&Deleted_Date='.$d.'"><img src="images/b_insrow.png" width="25"/></a></td>';
 }
if($q=="deleted_resource_team")
{
$s=$row['Training_Id'];
$d=$row['Deleted_date'];
$c=$row['T_NIC_Number'];
	$hint=$hint.'<td width="20"><a href="index.php?action=deletert&T_NIC_Number='.$c.'&Training_Id='.$s.'&Deleted_Date='.$d.'"><img src="images/b_deltbl.png" width="25"/></a></td>';
    $hint=$hint.'<td width="20"><a href="index.php?action=addrt&T_NIC_Number='.$c.'&Training_Id='.$s.'&Deleted_Date='.$d.'"><img src="images/b_insrow.png" width="25"/></a></td>';
}
if($q=="deleted_lab")
{
$s=$row['School_Id'];
$d=$row['Deleted_date'];
$c=$row['Lab_Id'];
		$hint=$hint.'<td width="29"><a href="index.php?action=deletel&Lab_Id='.$c.'&School_Id='.$s.'&Deleted_Date='.$d.'"><img src="images/b_deltbl.png" width="25"/></a></td>';
    $hint=$hint.'<td width="29"><a href="index.php?action=addl&Lab_Id='.$c.'&School_Id='.$s.'&Deleted_Date='.$d.'"><img src="images/b_insrow.png" width="25"/></a></td>';
}
$gettable="SELECT * FROM $q";
$gettables=mysql_query($gettable);
	$cnamearray=explode(",",$colnamearray);
	if($q=="deleted_lab")
	{
	if($row=mysql_fetch_array($gettables))
	{
	for($cv=1;$cv<=10;$cv++)
	{
	$val=$cnamearray[$cv];
	$hint=$hint.'<td>'.$row[$val].'</td>';
	}
	}
	}
	else if($q=="deleted_teacher")
	{
	if($row=mysql_fetch_array($gettables))
	{
	for($cv=1;$cv<=21;$cv++)
	{
	$val=$cnamearray[$cv];
	$hint=$hint.'<td>'.$row[$val].'</td>';
	}
	}
	}
	else
	{
	if($row=mysql_fetch_array($gettables))
	{
	for($cv=1;$cv<=$totcol;$cv++)
	{
	$val=$cnamearray[$cv];
	$hint=$hint.'<td>'.$row[$val].'</td>';
	}
	}
	}
	$hint=$hint."</tr>";
}
$hint=$hint."</table>";
   $hint=$hint.'</div>';
   if($next!="")
   {
   $hint=$hint.'<input type="button" class="submit" onClick="showDeltable(delTable.value)" align="right"/>';
      }
	  if($back!="")
   {
	  $hint=$hint.'<input type="button" class="submit2" onClick="showDeltable(backTable.value)" align="left"/>';
	  }
}
}

if($hint=="")
{
$response="No suggestions";
}
else
{
$response=$hint;
}
echo $response;
?>